package com.ruoyi.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.domain.BasisGroundEnvironment;
import com.ruoyi.domain.BasisLoadingEnvironment;
import com.ruoyi.mapper.BasisCampMapper;
import com.ruoyi.mapper.BasisGroundEnvironmentMapper;
import com.ruoyi.mapper.BasisLoadingEnvironmentMapper;
import com.ruoyi.service.IBasisGroundEnvironmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 阵地环境Service业务层处理
 * 
 * @author wangzebing
 * @date 2025-05-11
 */
@Service
public class BasisGroundEnvironmentServiceImpl implements IBasisGroundEnvironmentService
{
    @Autowired
    private BasisGroundEnvironmentMapper basisGroundEnvironmentMapper;

    @Autowired
    private BasisLoadingEnvironmentMapper basisLoadingEnvironmentMapper;



    /**
     * 查询阵地环境
     * 
     * @param id 阵地环境主键
     * @return 阵地环境
     */
    @Override
    public BasisGroundEnvironment selectBasisGroundEnvironmentById(Long id)
    {
        return basisGroundEnvironmentMapper.selectBasisGroundEnvironmentById(id);
    }

    /**
     * 查询阵地环境列表
     * 
     * @param basisGroundEnvironment 阵地环境
     * @return 阵地环境
     */
    @Override
    public List<BasisGroundEnvironment> selectBasisGroundEnvironmentList(BasisGroundEnvironment basisGroundEnvironment)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        basisGroundEnvironment.setUserId(user.getUserId());
        return basisGroundEnvironmentMapper.selectBasisGroundEnvironmentList(basisGroundEnvironment);
    }

    @Override
    public List<BasisGroundEnvironment> findAllBasisGroundEnvironmentList(BasisGroundEnvironment basisGroundEnvironment) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        basisGroundEnvironment.setUserId(user.getUserId());
        return basisGroundEnvironmentMapper.findAllBasisGroundEnvironmentList(basisGroundEnvironment);
    }

    /**
     * 新增阵地环境
     * 
     * @param basisGroundEnvironment 阵地环境
     * @return 结果
     */
    @Override
    public int insertBasisGroundEnvironment(BasisGroundEnvironment basisGroundEnvironment)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        basisGroundEnvironment.setUserId(user.getUserId());
        basisGroundEnvironment.setCreateTime(new Date());

        int i = basisGroundEnvironmentMapper.insertBasisGroundEnvironment(basisGroundEnvironment);
        if (i > 0){
//            BasisLoadingEnvironment basisLoadingEnvironment = new BasisLoadingEnvironment();
//            basisLoadingEnvironment.setGroundEnvironmentId(basisGroundEnvironment.getId());
//            basisLoadingEnvironment.setCaseLoadingId(basisGroundEnvironment.getCaseLoadingId());
//            int i1 = basisLoadingEnvironmentMapper.insertBasisLoadingEnvironment(basisLoadingEnvironment);


            boolean b = basisGroundEnvironmentMapper.addBasisLoadingGroundEnvironmentCamp(basisGroundEnvironment);
            if(b == true)  return 1;;

        }

        return i;
    }

    /**
     * 修改阵地环境
     * 
     * @param basisGroundEnvironment 阵地环境
     * @return 结果
     */
    @Override
    public int updateBasisGroundEnvironment(BasisGroundEnvironment basisGroundEnvironment)
    {
        return basisGroundEnvironmentMapper.updateBasisGroundEnvironment(basisGroundEnvironment);
    }

    /**
     * 批量删除阵地环境
     * 
     * @param ids 需要删除的阵地环境主键
     * @return 结果
     */
    @Override
    public int deleteBasisGroundEnvironmentByIds(Long[] ids,String loadingId)
    {

        int i = basisGroundEnvironmentMapper.deleteBasisGroundEnvironmentByIds(ids);

        if (i>0){
            for (int i1 = 0; i1 < ids.length; i1++) {
              return basisLoadingEnvironmentMapper.deleteBasisLoadingEnvironmentById(Math.toIntExact(ids[i1]),loadingId);
            }
        }
        return i;
    }

    /**
     * 删除阵地环境信息
     * 
     * @param id 阵地环境主键
     * @return 结果
     */
    @Override
    public int deleteBasisGroundEnvironmentById(Long id)
    {
        return basisGroundEnvironmentMapper.deleteBasisGroundEnvironmentById(id);
    }
}
